package com.guida.controller;

import com.guida.domain.Book;
import com.guida.domain.User;
import com.guida.entity.PageResult;
import com.guida.entity.Result;
import com.guida.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/book")
public class BookController {

    @Autowired
    private BookService bookService;

    @RequestMapping("/selectNewbooks")
    public ModelAndView selectNewBooks(){

        PageResult pageResult = bookService.selectNewBooks(1,13);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("books_new");
        modelAndView.addObject("pageResult",pageResult);
        return modelAndView;
    }

    //根据id查询图书信息
    @ResponseBody
    @RequestMapping("/book/findById")
    public Result<Book> findById(String id){
        try {
            Book book = bookService.findById(id);
            if (book == null){
                return new Result(false,"查询图书失败");
            }
            return new Result(true,"查询图书成功",book);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"查询图书失败");
        }
    }

    @ResponseBody
    @RequestMapping("/book/borrowBook")
    public Result borrowBook(Book book, HttpSession session){
        //获取当前登录用户的用户
        String pname = ((User)session.getAttribute("USER_SESSION")).getName();
        book.setBorrower(pname);
        System.out.println(pname);
        try{
            Integer count = bookService.borrowBook(book);

            if (count != 1){
                return new Result(false,"借阅图书失败");
            }
            return new Result(true,"借阅成功，请去领取图书",book);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"借阅图书失败");
        }
    }


    //查询图书:分页查询符合条件而未下架的图书信息
    /**
     * book:查询条件封装到book中
     * pageNumber:数据列表的当前页码
     * pageSize：当前列表展示多少条数据
     */
    @RequestMapping({"/search","/book/search"})
    public ModelAndView search(Book book, Integer pageNumber, Integer pageSize, HttpServletRequest request){
        if (pageNumber == null){
            pageNumber = 1;
        }
        if (pageSize == null){
            pageSize = 10;
        }

        //查询到图书信息
        PageResult pageResult = bookService.search(book,pageNumber,pageSize);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("books");
        //将查询到的数据存放到MAV中
        modelAndView.addObject("pageResult",pageResult);
        //将查询到的页数返回到页面，用于回显到查询的显示框中
        modelAndView.addObject("search",book);
        //将当前页码返回到页面,用于分页插件的显示
        modelAndView.addObject("pageNum",pageNumber);
        //将查询的控制器路径返回到页面,页码变化时继续向该路径发送请求
        modelAndView.addObject("gourl",request.getRequestURI());
        return modelAndView;
    }

    /**
     * 新增图书
     * book：新增图书信息
     * 将新增的结果和像页面传递信息封装到Result对象中返回
     */
    @ResponseBody
    @RequestMapping("/book/addBook")
    public Result addBook(Book book){
        try {
            Integer count = bookService.addBook(book);
            if (count != 1){
                return new Result(false,"新增图书失败");
            }
            return new Result(true,"新增图书成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"异常，新增图书失败");
        }
    }

    /**
     * 编辑图书信息
     * 页面效果：点击编辑，输入信息后，图书信息模态框将会自动隐藏，由于绑定了鼠标单击事件，触发事件
     * 就会调用my.js文件中的addOrEdit方法
     */
    @ResponseBody
    @RequestMapping("/book/editBook")
    public Result editBook(Book book){
        try {
            Integer count = bookService.editBook(book);
            if (count != 1){
                return new Result(false,"编辑失败");
            }
            return new Result(true,"编辑成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"异常，编辑失败");
        }
    }


    /**
     * 分页查询当前被借阅未归还的图书
     * @param book：图书信息
     * @param pageNum：数据列表当前页码
     * @param pageSize：数据列表1页展示多少条数据
     * @return
     */
    @RequestMapping({"/book/searchBorrowed","/searchBorrowed"})
    public ModelAndView searchBorrowed(Book book, Integer pageNum, Integer pageSize, HttpServletRequest request) {
        //默认显示第一页
        if (pageNum == null){
            pageNum = 1;
        }
        //默认当前显示10页
        if (pageSize == null){
            pageSize = 10;
        }
        //获取当前登录的用户
        User user = (User) request.getSession().getAttribute("USER_SESSION");
        PageResult pageResult = bookService.searchBorrowed(book, user, pageNum, pageSize);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("book_borrowed");
        //将查询到的数据存放在modelAndView对象中
        modelAndView.addObject("pageResult",pageResult);
        //将查询的参数返回到页面，用于回显到查询的输入框
        modelAndView.addObject("search",book);
        //将当前页码返回到页面,用于分页插件的显示
        modelAndView.addObject("pageNum",pageNum);
        //将当前查询的控制器路径返回到页面，页面变化时继续向该路径发送请求
        modelAndView.addObject("gourl",request.getRequestURI());
        return modelAndView;
    }

    /**
     * 归还图书
     * @param id:归还图书id
     * 获取当前用户登录的信息，将归还图书的id和当前登录用户信息作为returnBook方法的实参
     * 通过bookService对象调用returnBook方法执行归还图书操作，并将结果返回。
     */

    @ResponseBody
    @RequestMapping("/book/returnBook")
    public Result returnBook(String id, HttpSession session){
        //获取当前登录的用户信息
        User user = (User)session.getAttribute("USER_SESSION");
        try {
            boolean flag = bookService.returnBook(id,user);
            if (!flag){
                return new Result(false,"还书失败");
            }
            return new Result(true,"还书确认中，请先到管理处归还图书");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"还书失败");
        }
    }

    @ResponseBody
    @RequestMapping("/book/returnConfirm")
    public Result returnConfirm(String id){
        try {
            Integer count = bookService.returnConfirm(id);
            if (count != 1){
                return new Result(false,"确认归还失败");
            }
            return new Result(false,"确认归还成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"异常，确认归还失败");
        }
    }

}
